package com.weijian.controller.admin;

import com.weijian.common.enums.GlobalErrorCodeConstants;
import com.weijian.common.exception.ServiceException;
import com.weijian.common.pojo.CommonResult;
import com.weijian.dal.query.UserRegisterQuery;
import com.weijian.dal.vo.UserVO;
import com.weijian.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户接口
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://yupi.icu">编程导航知识星球</a>
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public CommonResult<Long> userRegister(@RequestBody UserRegisterQuery UserRegisterQuery) {
        if (UserRegisterQuery == null) {
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        String userAccount = UserRegisterQuery.getUserAccount();
        String userPassword = UserRegisterQuery.getUserPassword();
        String checkPassword = UserRegisterQuery.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            return null;
        }
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return CommonResult.success(result);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public CommonResult<UserVO> userLogin(@RequestBody UserRegisterQuery UserRegisterQuery, HttpServletRequest request) {
        if (UserRegisterQuery == null) {
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        String userAccount = UserRegisterQuery.getUserAccount();
        String userPassword = UserRegisterQuery.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        UserVO UserVO = userService.userLogin(userAccount, userPassword, request);

        return CommonResult.success(UserVO);
    }

}
